<?php
namespace Admin\Controller;
require VENDOR_PATH.'/jpush-api-php/autoload.php';
use JPush\Client as JPush;
use Common\Controller\AdminbaseController;

class PushController extends AdminbaseController {

    private  $app_key = '34c227e26b940424a0a5a6e4';

    private $master_secret = 'b543b4ac5178c3a88984688f';


    public function pushOrder($title,$Alias,$tag,$id){
        $client = new JPush($this->app_key, $this->master_secret);
        try {
            $response = $client->push()
                ->setPlatform(array('ios', 'android'))
//                ->addTag(array($tag))
                  ->addAlias($Alias)
                  ->setAudience('all')
                  ->setNotificationAlert($title)
                  ->iosNotification($title, array(
                    'sound' => $title,
                    'badge' => 1,
                    'content-available' => true,
                    'extras' => array(
                        'message' => 3,
                        'video_id' => $id,
                    ),
                ))
                ->androidNotification($title, array(
                    'title' => $title,
                   // 'build_id' => 2,
                    'extras' => array(
                        'message' => 3,
                        'video_id' => $id,

                    ),
                ))
               /* ->message($Notification, array(
                    'title' => $Notification,
                    'extras' => array(
                        'content_type' => 'text',
                        'message' => 3,
                        'product_id' => $id,
                        'code'=>'3',
                    ),
                ))*/
                ->options(array(
                    //   'time_to_live' => 86400,
                    'apns_production' => true,
                ))
                ->send();

        } catch (\JPush\Exceptions\APIConnectionException $e) {
            response(-2,array('msg'=>$e),'对方没有登录');
        } catch (\JPush\Exceptions\APIRequestException $e) {
            response(-2,array('msg'=>$e),'对方没有登录');
        }


        $report = $client->report();

        $rep = $report->getReceived($response['body']['msg_id']);
//            推送成功 更改状态
        if ($rep['http_code']==200)
        {
            return true;
        }else{
            return false;
        }
        //return true;
    }


    /**
     *查询推送消息
     *token
     */
    public function queryPushLog(){

        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);


        $log  = M('push_log');

        $map['uid'] = API_UID;

        $data = $log->field('meter_no,content,create_time')->where($map)->order('create_time desc')->select();

        empty($data)?response(-500):response(200,$data);

    }



    /**
     * 发送推送给配送商家
     * @param $message
     * @return int
     */
    public function sendToPser($title,$Alias,$id)
    {
        $client=new JPush($this->app_key,$this->master_secret);
        try {
            $response = $client->push()
                ->setPlatform(array('ios', 'android'))
                ->addAlias($Alias)
                ->setAudience('all')
                ->setNotificationAlert($title)
                ->iosNotification($title, array(
                    'sound' => $title,
                    'badge' => 1,
                    'content-available' => true,
                    'extras' => array(
                        'message' => 3,
                        'video_id' => $id,
                    ),
                ))
                ->androidNotification($title, array(
                    'title' => $title,
                    'extras' => array(
                        'message' => 3,
                        'video_id' => $id,

                    ),
                ))
                /* ->message($Notification, array(
                     'title' => $Notification,
                     'extras' => array(
                         'content_type' => 'text',
                         'message' => 3,
                         'product_id' => $id,
                         'code'=>'3',
                     ),
                 ))*/
                ->options(array(
                    'apns_production' => true,
                ))
                ->send();

        } catch (\JPush\Exceptions\APIConnectionException $e) {
           // response(-2,array('msg'=>$e),'对方没有登录');
            return false;
        } catch (\JPush\Exceptions\APIRequestException $e) {
         //   response(-2,array('msg'=>$e),'对方没有登录');
            return false;
        }


        $report = $client->report();

        $rep = $report->getReceived($response['body']['msg_id']);

        //推送成功 更改状态
        if ($rep['http_code']==200)
        {
            return true;
        }else{
            return false;
        }

    }

    

}
?>